// SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
// SPDX-License-Identifier: Apache-2.0

package resource_dao

import (
	"context"
	"fmt"

	"gitcode.com/lvupclub/lvup-api/dao/common"
	"go.mongodb.org/mongo-driver/v2/bson"
	"go.mongodb.org/mongo-driver/v2/mongo"
	"go.mongodb.org/mongo-driver/v2/mongo/options"
)

type QuestionStateItem struct {
	ObjectId     string `bson:"_id"`
	MemberUserId string `bson:"memberUserId"`
	ResourceId   string `bson:"resourceId"`
	QuestionId   string `bson:"questionId"`
	AnswerData   []byte `bson:"answerData"`
	AnswerTime   int64  `bson:"answerTime"`
}

type _QuestionStateDao struct {
	mc *mongo.Collection
}

func newQuestionStateDao(db *mongo.Database) (*_QuestionStateDao, error) {
	mc := db.Collection(common.DB_NS_RESOURCE + "question_state")
	_, err := mc.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
		{
			Keys: bson.D{{"memberUserId", 1}, {"resourceId", 1}},
		},
		{
			Keys: bson.D{{"resourceId", 1}, {"questionId", 1}},
		},
	})
	if err != nil {
		return nil, err
	}
	return &_QuestionStateDao{mc: mc}, nil
}

func (db *_QuestionStateDao) genObjectId(memberUserId, resourceId, questionId string) string {
	return fmt.Sprintf("%s:%s:%s", memberUserId, resourceId, questionId)
}

func (db *_QuestionStateDao) Set(ctx context.Context, dbItem *QuestionStateItem) error {
	dbItem.ObjectId = db.genObjectId(dbItem.MemberUserId, dbItem.ResourceId, dbItem.QuestionId)
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": dbItem.ObjectId}, bson.M{
		"$set": dbItem,
	}, options.UpdateOne().SetUpsert(true))
	return err
}

func (db *_QuestionStateDao) Exist(ctx context.Context, memberUserId, resourceId, questionId string) (bool, error) {
	objectId := db.genObjectId(memberUserId, resourceId, questionId)
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": objectId})
	return count > 0, err
}

func (db *_QuestionStateDao) Get(ctx context.Context, memberUserId, resourceId, questionId string) (*QuestionStateItem, error) {
	objectId := db.genObjectId(memberUserId, resourceId, questionId)
	retItem := &QuestionStateItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": objectId}).Decode(retItem)
	return retItem, err
}

func (db *_QuestionStateDao) Count(ctx context.Context, memberUserId, resourceId string) (uint32, error) {
	query := bson.M{"memberUserId": memberUserId, "resourceId": resourceId}
	count, err := db.mc.CountDocuments(ctx, query)
	return uint32(count), err
}

func (db *_QuestionStateDao) List(ctx context.Context, memberUserId, resourceId string) ([]*QuestionStateItem, error) {
	query := bson.M{"memberUserId": memberUserId, "resourceId": resourceId}
	cursor, err := db.mc.Find(ctx, query)
	if err != nil {
		return nil, err
	}
	retList := []*QuestionStateItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_QuestionStateDao) RemoveByQuestion(ctx context.Context, resourceId, questionId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"resourceId": resourceId, "questionId": questionId})
	return err
}

func (db *_QuestionStateDao) RemoveByResource(ctx context.Context, resourceId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"resourceId": resourceId})
	return err
}
